/*! ******************************************************************************
 *
 * Created on 2021年9月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.eclipse.swt.widgets;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.yzbdl.lanius.core.plugins.metastore.LdrPluginEntity;
import org.pentaho.di.ui.spoon.navigation.design.AbstractTree;

/**
 * @author tanpeng@yzbdl.ac.cn
 * @version 1.0
 * @date 2022-08-02 11:19
 */
public class LaniusPluginTreeItem extends TreeItem {

	private LdrPluginEntity ldrPluginEntity;

	public String destText;
	public Image destImage;

	public LaniusPluginTreeItem(Tree parent, int style, LdrPluginEntity ldrPluginEntity, String name, Image image) {
		super(parent, style);

		this.ldrPluginEntity = ldrPluginEntity;
		this.text = name;
		this.image = image;
		this.destText = name;
		this.destImage = image;

		this.addDisposeListener(new LaniusPluginTreeItemDisposeListener());
	}

	public String getDestText() {
		return this.destText;
	}

	public Image getDestImage() {
		return this.destImage;
	}

	public LaniusPluginTreeItem(TreeItem parentItem, int style, LdrPluginEntity ldrPluginEntity, String name, Image image) {
		super(parentItem, style);

		this.ldrPluginEntity = ldrPluginEntity;
		this.text = name;
		this.image = image;
		this.destText = name;
		this.destImage = image;

		this.addDisposeListener(new LaniusPluginTreeItemDisposeListener());
	}

	public LdrPluginEntity getPluginEntity() {
		return ldrPluginEntity;
	}

	public void setPluginEntity(LdrPluginEntity ldrPluginEntity) {
		this.ldrPluginEntity = ldrPluginEntity;
	}

	class LaniusPluginTreeItemDisposeListener implements DisposeListener {
		@Override
		public void widgetDisposed(DisposeEvent e) {
			LaniusPluginTreeItem source = (LaniusPluginTreeItem) e.getSource();
			LdrPluginEntity ldrPluginEntity = source.getPluginEntity();
			String key = ldrPluginEntity.getPluginTypeClass() + "_" + ldrPluginEntity.getPluginId() + "_" + ldrPluginEntity.getVersion();
			AbstractTree.rtPluginTreeItemMap.get(key).remove(source);
		}
	}

}
